home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12984 / 12984.xpi / chrome / VideoDownloaderToolbar.jar / content / queue.js < prev    next >
Text File  |  2010-01-29  |  5KB  |  180 lines

  1. if(!com) var com={};
  2. if(!com.VidBar) com.VidBar={};
  3.  
  4. com.VidBar.DownloadQueue = function() {
  5. }
  6.  
  7. com.VidBar.DownloadQueue.prototype = {
  8.     downloader : null,
  9.     downloadQueue : null,
  10.     db:null,
  11.     onLoad : function(event) {
  12.         this.db = new com.VidBar.VidDB();
  13.         this.dlMgr = Components.classes["@mozilla.org/download-manager;1"]
  14.                     .getService(Components.interfaces.nsIDownloadManager);
  15.  
  16.         this.update();
  17.         this.syncDownloads();
  18.         
  19.         var _this = this;
  20.         this.updateTimer = setInterval(function() {
  21.                     _this.update();
  22.                 }, 1000);
  23.         this.syncTimer = setInterval(function() {
  24.                     _this.syncDownloads();
  25.                 }, 3000);
  26.     },
  27.     onUnload : function(event) {
  28.         if (this.updateTimer) {
  29.             clearInterval(this.updateTimer);
  30.         }
  31.         if (this.syncTimer) {
  32.             clearInterval(this.syncTimer);
  33.         }
  34.     },
  35.     syncDownloads : function(){
  36.         this.db.syncDownloads(this.dlMgr);
  37.     },
  38.     update : function() {
  39.         this.updateWaitQueue();
  40.         this.updateDownloadList();
  41.     },
  42.     updateWaitQueue : function() {
  43.         var children = document.getElementById("queueChildren");
  44.         var queue = this.db.getWaitings();
  45.         var items = [];
  46.         for (var i = 0; i < queue.length; i++) {
  47.             var entry = queue[i];
  48.             var item = document.getElementById("queue-" + entry.guid);
  49.             var filenameCell, sizeCell;
  50.             if (item) {
  51.                 // found, update
  52.                 var filenameCell = item.childNodes[0].childNodes[0];
  53.                 sizeCell = filenameCell.nextSibling;
  54.             } else {
  55.                 // not found, create
  56.                 item = document.createElement("treeitem");
  57.                 item.setAttribute("id", "queue-" + entry.guid);
  58.                 var row = document.createElement("treerow");
  59.                 var filenameCell = document.createElement("treecell");
  60.                 row.appendChild(filenameCell);
  61.                 sizeCell = document.createElement("treecell");
  62.                 row.appendChild(sizeCell);
  63.                 item.appendChild(row);
  64.                 children.appendChild(item);
  65.             }
  66.             filenameCell.setAttribute("label", entry.filename);
  67.             sizeCell.setAttribute("label", VidUtils.getSizeStr(entry.size));
  68.             items.push(item);
  69.         }
  70.         var removeList = [];
  71.         for (var i = 0; i < children.childNodes.length; i++) {
  72.             var found = false;
  73.             for (var j = 0; j < items.length; j++) {
  74.                 if (children.childNodes[i] == items[j]) {
  75.                     found = true;
  76.                     break;
  77.                 }
  78.             }
  79.             if (!found) {
  80.                 removeList.push(children.childNodes[i]);
  81.             }
  82.         }
  83.         for (var i = 0; i < removeList.length; i++) {
  84.             children.removeChild(removeList[i]);
  85.         }
  86.     },
  87.     updateDownloadList : function() {
  88.         var children = document.getElementById("downChildren");
  89.         var list = this.db.getDownloadings();
  90.         var items = [];
  91.         for (var i = 0; i < list.length; i++) {
  92.             var entry = list[i];
  93.             var item = document.getElementById("down-" + entry.guid);
  94.             var filenameCell, statusCell, sizeCell, percentCell;
  95.             if (item) {
  96.                 // found, update
  97.                 var filenameCell = item.childNodes[0].childNodes[0];
  98.                 statusCell = filenameCell.nextSibling;
  99.                 sizeCell = statusCell.nextSibling;
  100.                 percentCell = sizeCell.nextSibling;
  101.             } else {
  102.                 // not found, create
  103.                 item = document.createElement("treeitem");
  104.                 item.setAttribute("id", "down-" + entry.guid);
  105.                 var row = document.createElement("treerow");
  106.                 filenameCell = document.createElement("treecell");
  107.                 row.appendChild(filenameCell);
  108.                 statusCell = document.createElement("treecell");
  109.                 row.appendChild(statusCell);
  110.                 sizeCell = document.createElement("treecell");
  111.                 row.appendChild(sizeCell);
  112.                 percentCell = document.createElement("treecell");
  113.                 row.appendChild(percentCell);
  114.                 item.appendChild(row);
  115.                 children.appendChild(item);
  116.             }
  117.             filenameCell.setAttribute("label", entry.filename);
  118.             var statusDisplay = VidUtils.getDownloadStatusDisplay(entry.status);
  119.             statusCell.setAttribute("label", statusDisplay[0]);
  120.             sizeCell.setAttribute("label", VidUtils.getSizeStr(entry.size));
  121.             percentCell.setAttribute("label", (entry.percentComplete||"0") + "%");
  122.             items.push(item);
  123.         }
  124.         var removeList = [];
  125.         for (var i = 0; i < children.childNodes.length; i++) {
  126.             var found = false;
  127.             for (var j = 0; j < items.length; j++) {
  128.                 if (children.childNodes[i] == items[j]) {
  129.                     found = true;
  130.                     break;
  131.                 }
  132.             }
  133.             if (!found) {
  134.                 removeList.push(children.childNodes[i]);
  135.             }
  136.         }
  137.         for (var i = 0; i < removeList.length; i++) {
  138.             children.removeChild(removeList[i]);
  139.         }
  140.     },
  141.     onSelect:function(){
  142.         var enableRemove = false;
  143.         var tree = document.getElementById("vidbar-wait-queue");
  144.         if(tree.view==null)
  145.             enableRemove = false;
  146.         else if(tree.view.selection.count>0)
  147.             enableRemove = true;
  148.         document.getElementById("vidbar-queue-remove").disabled = !enableRemove;
  149.     },
  150.     removeFromQueue : function() {
  151.         var tree = document.getElementById("vidbar-wait-queue");
  152.         if (tree.view == null)
  153.             return;
  154.         else if (tree.view.selection.count == 0)
  155.             return;
  156.             
  157.         var sel = [];
  158.         var numRanges = tree.view.selection.getRangeCount();
  159.         for (var t = 0; t < numRanges; t++) {
  160.             var start = {};
  161.             var end = {};
  162.             tree.view.selection.getRangeAt(t, start, end);
  163.             for (var v = start.value; v <= end.value; v++) {
  164.                 if (v >= 0) {
  165.                     var item = tree.view.getItemAtIndex(v);
  166.                     var id = item.getAttribute("id");
  167.                     if(id.substr(0, 6)=="queue-"){
  168.                         sel.push(id.substr(6));
  169.                     }
  170.                 }
  171.             }
  172.         }
  173.         com.VidBar.__d("removeFromQueue: selected items: "+sel);
  174.         this.db.removeWaitings(sel);
  175.     },
  176.     cleanQueue:function(){
  177.         this.db.removeAllWaitings();
  178.     }
  179. };
  180.